namespace Leo.Core.Util
{
    public static class DateTimeUtil
    {
        /// <summary>
        ///     时间戳起始日期
        /// </summary>
        public static DateTime TimestampStart = new(1970, 1, 1, 0, 0, 0, 0);

        public static long Timestamp => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

        /// <summary>
        ///     获取时间戳
        /// </summary>
        /// <param name="milliseconds">是否使用毫秒</param>
        /// <returns></returns>
        public static string GetTimeStamp(bool milliseconds = false)
        {
            var ts = DateTime.UtcNow - TimestampStart;
            return Convert.ToInt64(milliseconds ? ts.TotalMilliseconds : ts.TotalSeconds).ToString();
        }

        /// <summary>
        ///     时间戳转本地时间-时间戳精确到秒
        /// </summary>
        public static DateTime ToLocalTimeDateBySeconds(long unix)
        {
            var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
            return dto.ToLocalTime().DateTime;
        }

        /// <summary>
        ///     时间转时间戳Unix-时间戳精确到秒
        /// </summary>
        public static long ToUnixTimestampBySeconds(DateTime dt)
        {
            DateTimeOffset dto = new(dt);
            return dto.ToUnixTimeSeconds();
        }

        /// <summary>
        ///     时间戳转本地时间-时间戳精确到毫秒
        /// </summary>
        public static DateTime ToLocalTimeDateByMilliseconds(long unix)
        {
            var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
            return dto.ToLocalTime().DateTime;
        }

        /// <summary>
        ///     时间转时间戳Unix-时间戳精确到毫秒
        /// </summary>
        public static long ToUnixTimestampByMilliseconds(DateTime dt)
        {
            DateTimeOffset dto = new(dt);
            return dto.ToUnixTimeMilliseconds();
        }
    }
}